<?php
  /** Abstraction des catégories d'équipes
   * @author sebastian mosser
   */

class TeamCategory implements iPersistantData, iHTMLViewable, 
															iLookable, iXMLSerializable {

	private $_id;       ///< identifiant 
	private $_label;    ///< label (nom) de la catégorie 
	private $_director; ///< id du Member associé a la direciton de la catégorie
	private $_user;     ///< id du User administrant la catégorie
	private $_season;   ///< id de la saison associée a cette entitée


  public function __construct($director, $user, $label, $season, $id = -1) {
		$this->_id       = $id;
		$this->_director = $director;
		$this->_user     = $user;
		$this->_label    = $label;
		$this->_season   = $season;
  }

	public function xmlExport($e,$t="") {
		$id = $this->_id;
		$label = htmlentities($this->_label,ENT_COMPAT,"UTF-8");
		$director = htmlentities($this->_director,ENT_COMPAT,"UTF-8");
		$ans = <<<EOS
  <team_category id="${id}">
  	<name>${label}</name>
  	<director>${director}</director>
  </team_category>
EOS;
		$e->insertData("category",$this->_id,$ans);
		$director = $e->visitMember($this->_director,"coach");
	}
	
	public function getUser() { return $this->_user; }

	/** Persistance des données **/
	public static function createTable() {
	  $sql  = "CREATE TABLE IF NOT EXISTS `team_category` (\n";
	  $sql .= "  `id`  INTEGER NOT NULL PRIMARY KEY auto_increment,  \n";
 	  $sql .= "  `label`       VARCHAR(50) NOT NULL, \n                ";
	  $sql .= "  `director_id` INTEGER NOT NULL REFERENCES `member`, \n";
	  $sql .= "  `user_id`     INTEGER NOT NULL REFERENCES `user`,   \n";
	  $sql .= "  `season_id`   INTEGER NOT NULL REFERENCES `season`);";

	  $myDb = Database::getSingleton();
	  $myDb->execute($sql);
	}

	public static function dropTable() {
		$sql = "DROP TABLE IF EXISTS `team_category`";
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	}
	
	public function create() {
	  if ($this->_id != -1)
			throw new RuntimeException("TeamCategory/create()","still present in db");

		$label    = Database::escape($this->_label);
		$director = Database::escape($this->_director);
		$user     = Database::escape($this->_user);
		$season   = Database::escape($this->_season);

		$sql  = "INSERT INTO `team_category` VALUES (\n";
		$sql .= "  '', '$label', '$director', '$user', '$season');";

		$myDb = Database::getSingleton();
		$ans = $myDb->executeInsert($sql);
		if ($ans[0] != 0)
			$this->_id = $ans[0];
		else
			$this->_id = -1;
	}
	
	public static function read($key) {
		$key = Database::escape($key);
		$sql = "SELECT * FROM `team_category` WHERE `id` = $key";
		$myDb = Database::getSingleton();
		$ans = Database::assoc($myDb->execute($sql));
		$aTeamCategory = new TeamCategory($ans["director_id"], $ans["user_id"], 
														$ans["label"], $ans["season_id"], $ans["id"]);
		return $aTeamCategory;
	}
	
	public function update() {
	
		if ($this->_id == -1)
			throw new RuntimeException("TeamCategory/update()", "doesn't exists !!");
		
		$id       = Database::escape($this->_id);
		$label    = Database::escape($this->_label);
		$director = Database::escape($this->_director);
		$user     = Database::escape($this->_user);
		$season   = Database::escape($this->_season);
	
		$sql  = "UPDATE `team_category` SET     \n";
		$sql .= "  `label`       = '$label',    \n";
		$sql .= "  `director_id` = '$director', \n";
		$sql .= "  `user_id`     = '$user',     \n";
		$sql .= "  `season_id`   = '$season'    \n";
		$sql .= "  WHERE `id` = $id ;";
		
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
		
	}
	
	public function delete() {
		if ($this->_id == -1)
			throw new RuntimeException("TeamCategory/delete()","doesn't exists");
		$sql = "DELETE FROM `team_category` WHERE `id` = $this->_id ;";
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	}
	
	public function tag() {
		$tmp = array();
		$tmp[0] = "TeamCategory";
		$tmp[1] = $this->_id;
		return $tmp;
	}
	
	public function soundsLike($o) {
		return ($this->_label == $o->_label && $this->_director == $o->_director); 
	}
	
	/** Fin de la persistance des données **/
	
	/** Interface de recherche **/
	
	// Le filtre est le numéro de saison voulu
	public static function lookFor($filter) {
		$season = Database::escape($filter);
		$sql = "SELECT * FROM `team_category` WHERE `season_id` = $season";
		$myDb = Database::getSingleton();
		return $myDb->execute($sql);
	}
	
	public static function showResult($resultSet, $scriptUrl = "") {
		
		$content = "";
		while(($row = Database::assoc($resultSet)) != False) {
			$aTeamCategory = TeamCategory::read($row["id"]);
			$content .= "<tr> " . $aTeamCategory->displayInline($scriptUrl);
			$content .= "</tr>";
		}
		if ($content == "") {
			$result  = "<div align=\"center\"> <span class=\"error\">\n";
			$result .= "  Pas de catégories disponibles !! </span> </div>";
		}
		else {
			$result = <<<EOS
	<div align="center">
		<table class="resultset">
		<tr> <th> Nom de Catégorie </th> <th> Responsable Sportif </th> 
				 <th> Administrateur Balloon </th> </tr>
		${content}
		</table>
	</div>
EOS;
		}
		return $result;
	}

	/** Interface de vision HTML **/
	
	public function displayLong() {
		$season = Season::read($this->_season);
		return $season->displayShort() . " " . $this->_label;
	}
	
	public function displayShort() {
		return $this->_label;
	}
	
	public function displayInline($scriptUrl="") {
		$aUser = User::read($this->_user);
		$aMember = Member::read($this->_director);
		
		$user = $aUser->displayShort();
		$director = $aMember->getFirstName() ." " .$aMember->getLastName();
		$ans = "<td> $this->_label </td> <td> $director </td>";
		$ans .= " <td> <code> $user </code> </td>";
		return $ans;
	}
	
	
	/** Fin de la vision HTML**/


	public static function getAsList($season, $selected = -1) {
		$ans = self::lookFor($season);
		$i = 0;
		$content = "";
		while (($row = Database::assoc($ans)) != False) {
			$s = (($row["id"] == $selected) ? "selected" : "");
			$aCat = TeamCategory::read($row["id"]);
			$content .= "<option value=\"".$row["id"]."\" ${s}>";
			$content .= $aCat->displayShort() . "</option>"; 
			$i++;
		}
		if ($i == 0)
			return "<option value=\"-1\"> ??? </option>";
		else
			return $content;
	}
	
	//Accès en écriture aux variables
	
	public function setLabel($l)	{ $this ->_label = $l;		}
	public function setDirector($d)	{ $this ->_director = $d;	}
	public function setUser($u)		{ $this ->_user = $u;		}
	
}
?>